package com.briup.jz.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.Article;
import com.briup.jz.bean.ArticleExample;
import com.briup.jz.bean.extend.ArticleExtend;
import com.briup.jz.dao.ArticleMapper;
import com.briup.jz.dao.extend.ArticleExtendMapper;
import com.briup.jz.service.IArticleService;
import com.briup.jz.utils.CustomerException;

@Service
public class ArticleServiceImpl implements IArticleService {
	
//接口
	
	@Autowired
	private ArticleExtendMapper articleExtendMapper; //做复杂增删改查
	
	@Autowired
	private ArticleMapper articleMapper;

	@Override
	public void saveOrUpdate(Article article)  {
		if(article.getId() == null) {//id为空，做插入操作
			//初始化
			article.setPublishTime(new Date().getTime());
			article.setReadTimes(0l);//阅读次数
			article.setThumpUp(0l);//点赞次数
			article.setStatus("未审核");
			//插入到数据库
			articleMapper.insert(article);
		}else {
			articleMapper.updateByPrimaryKey(article);
		}
		}
		

		
	@Override	
	public void deleteById(long id) throws CustomerException {
		Article category = articleMapper.selectByPrimaryKey(id);
		if(category == null) {
			throw new CustomerException("要删除的分类信息不存在");
		}
		articleMapper.deleteByPrimaryKey(id);
		
	}


	@Override
	public List<Article> query(String title, String status, Long categoryId) {
		// TODO Auto-generated method stub
		//模板查询
		ArticleExample example = new ArticleExample();
		//条件
		ArticleExample.Criteria criteria = example.createCriteria();//模板对象
		if(title != null) {
			criteria.andTitleLike("%"+title+"%"); //模糊查询
		}
		if(status != null) {
			criteria.andStatusEqualTo(status); //准确字段查询
		}
		if(categoryId != null) {
			criteria.andCategoryIdEqualTo(categoryId);
				
			}
		
		
		return articleMapper.selectByExample(example);
}


	@Override
	public List<ArticleExtend> queryCascade(String title, String status, Long categoryId, Long authorId) {
		// TODO Auto-generated method stub
		return articleExtendMapper.select(title,status,categoryId,authorId);
	}
	}


